package com.tjcu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tjcu.entity.*;
import com.tjcu.service.StudentService;
import com.tjcu.service.impl.StudentServiceImpl;
import com.tjcu.utils.BirthdayUtil;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/11/3 16:01
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class StudentAction extends ActionSupport {
    private List students;
    private Integer pageNumber;
    /**
     * 总页数
     */
    private Integer totalPage;
    private List cnameAndGNames;
    private List<Mark> marks;
    private List<City> cities;
    private Student student;
    private String gcId;
    private String selectVal;
    private String selectName;

    public String showPage() {
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        students = studentService.showPage(pageNumber);
        System.out.println(students);
        totalPage = studentService.selectTotalPage();

        return SUCCESS;
    }

    /**
     * 学生添加功能需要展示的数据
     *
     * @return
     */
    public String showMsg() {
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        cnameAndGNames = studentService.showCnameAndGName();
        marks = studentService.showMarkName();
        cities = studentService.showCityName();
        return SUCCESS;
    }

    /**
     * 添加学生
     *
     * @return
     */
    public String addStudent() {
        System.out.println(student.getCity());
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        //学生的实体类：sid,sname,sphone,sqq,sage,sbirthday,sstars,sattribute,clazz, group,city,marks
        Date birthday = student.getSbirthday();
        //获取年龄
        student.setSage(BirthdayUtil.getAge(birthday));
        //获取生肖
        student.setSattribute(BirthdayUtil.getChineseZodiac(birthday));
        //获取星座
        student.setSstars(BirthdayUtil.getConstellation(birthday));
        //将班级和小组通过-进行拆分
        String[] strs = gcId.split("-");
        //通过Integer.parseInt()将String转化为Int类型
        student.setClazz(new Clazz(Integer.parseInt(strs[0]), null, null, null));
        student.setGroup(new Group(Integer.parseInt(strs[1]), null, null, null));
        System.out.println(student.getGroup());
        System.out.println(student.getClazz());
        System.out.println(student.getMarks());
        studentService.addStudent(student);
        return SUCCESS;
    }

    /**
     * 根据不同条件进行查询学生信息
     */
    public String showByNameOrPhoneOrQQ() {

        System.out.println("值" + selectVal);
        Student student1 = new Student();
        System.out.println(student1);
        String name = "name";
        String phone = "phone";
        String qq = "qq";
        if (selectName.equals(name)) {
            System.out.println("根据那个进行查询：" + selectName);
            ;
        }

        if (selectName.equals(name)) {
            student1.setSname(selectVal);

        } else if (selectName.equals(phone)) {
            student1.setSphone(selectVal);

        } else if (selectName.equals(qq)) {
            student1.setSqq(selectVal);

        }
        System.out.println(student1);
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        //condition条件 不同条件的学生
        students = studentService.showByNameOrPhoneOrQQ(student1);
        return SUCCESS;
    }
    //删除学生
    public String dropStudent() {
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        System.out.println(student.getSid());
        studentService.dropStudent(student.getSid());
        return SUCCESS;
    }


    /**
     * 通过id查询学生信息
     *
     */
    public String showStudent() {
        //调用业务层
        StudentService studentService = new StudentServiceImpl();
        student = studentService.selectStudentById(student.getSid());
        cnameAndGNames = studentService.showCnameAndGName();
        marks = studentService.showMarkName();
        cities = studentService.showCityName();
        System.out.println(student);
       return SUCCESS;
    }

    /**
     * 更新学生信息
     *
     */
    public String updateStudent() {
        //调用业务层
        StudentService studentService = new StudentServiceImpl();

        //学生的实体类：sid,sname,sphone,sqq,sage,sbirthday,sstars,sattribute,clazz, group,city,marks
        Date birthday = student.getSbirthday();
        //获取年龄
        student.setSage(BirthdayUtil.getAge(birthday));
        //获取生肖
        student.setSattribute(BirthdayUtil.getChineseZodiac(birthday));
        //获取星座
        student.setSstars(BirthdayUtil.getConstellation(birthday));
        //将班级和小组通过-进行拆分
        String[] strs = gcId.split("-");
        //通过Integer.parseInt()将String转化为Int类型
        student.setClazz(new Clazz(Integer.parseInt(strs[0]), null, null, null));
        student.setGroup(new Group(Integer.parseInt(strs[1]), null, null, null));
        studentService.updateStudent(student);
        return SUCCESS;
    }

    public List getStudents() {
        return students;
    }

    public void setStudents(List students) {
        this.students = students;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List getCnameAndGNames() {
        return cnameAndGNames;
    }

    public void setCnameAndGNames(List cnameAndGNames) {
        this.cnameAndGNames = cnameAndGNames;
    }

    public List<Mark> getMarks() {
        return marks;
    }

    public void setMarks(List<Mark> marks) {
        this.marks = marks;
    }

    public List<City> getCities() {
        return cities;
    }

    public void setCities(List<City> cities) {
        this.cities = cities;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public String getGcId() {
        return gcId;
    }

    public void setGcId(String gcId) {
        this.gcId = gcId;
    }

    public String getSelectVal() {
        return selectVal;
    }

    public void setSelectVal(String selectVal) {
        this.selectVal = selectVal;
    }

    public String getSelectName() {
        return selectName;
    }

    public void setSelectName(String selectName) {
        this.selectName = selectName;
    }
}
